package com.timer;
  
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

public class TimerView extends SurfaceView
                     implements SurfaceHolder.Callback {
    private SurfaceHolder holder;
    private boolean hasSurface;
    private Paint paint;  
    private int flag = 0;
    private Timer timerT;
    private Timer timerL;
    public int counter = 5;
    private int y=20;
    private int T1=65;
    private int T2=50;
    private int T3=55;
    private int T4=60;
    private int colorTop = Color.WHITE;
    private int colorLeft = Color.BLACK;
    private int colorRight = Color.WHITE;
    private int colorBottom = Color.BLACK;
    private int i = 5, j;
    private int screenHeight, screenWidth;
    //private TimerTop timerTop;

    TimerView(Context context) {
        super(context);
        
        paint = new Paint();
        timerT = new Timer();
        timerL = new Timer();
        DisplayMetrics metrics = new DisplayMetrics();
        ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);
        screenHeight = metrics.heightPixels;
        screenWidth = metrics.widthPixels;
        
        init();
    }

    // Implementing all callback methods below
    public void resume() {
        // Create and start the graphics update thread
        
    }

    public void pause() {
        // Stop the graphics update thread
        
    }

    public void surfaceCreated(SurfaceHolder holder) {
        hasSurface = true;
        Log.d("BouncingBallView", "TASK SHEDULED");
        timerT.schedule(new TimerTop(), 0, 5);
        //timerL.schedule(new TimerLeft(), 0, 1000);
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        hasSurface = false;
        pause();
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        
    }

    private void init() {
        // Create a new SurfaceHolder and assign this class as its callback
        holder = getHolder();
        holder.addCallback(this);
        hasSurface = false;
    }
    
    public void onDestroyApp(){
        Log.d("BouncingBallView", "onDestroy()");
        timerT.cancel();
        timerT.purge();
        //timerL.cancel();
        //timerL.purge();
    }

    public class TimerTop extends TimerTask{
        SurfaceHolder surfaceHolder = holder;
        
        public void run(){
            Canvas canvas = surfaceHolder.lockCanvas();
            j = 0;
            
            //this.setBackgroundColor(Color.GRAY);
            paint.setColor(colorTop);
            canvas.drawRect(screenWidth/2-50, 0, screenWidth/2+50, 50, paint);
            paint.setColor(colorLeft);
            canvas.drawRect(0, screenHeight/2-50, 50, screenHeight/2+50, paint);
            paint.setColor(colorRight);
            canvas.drawRect(screenWidth-50, screenHeight/2-50, screenWidth, screenHeight/2+50, paint);
            paint.setColor(colorBottom);
            canvas.drawRect(screenWidth/2-50, screenHeight-50, screenWidth/2+50, screenHeight, paint);
            
            if(i % T1 == 0){
                    if(colorTop == Color.WHITE)colorTop = Color.BLACK;
                    else colorTop = Color.WHITE;
                    j++;
                    //Log.d("BouncingBallActivity", "Test"+T1);
            }
            if(i % T2 == 0){
                    if(colorLeft == Color.WHITE)colorLeft = Color.BLACK;
                    else colorLeft = Color.WHITE;
                    j++;
                    //Log.d("BouncingBallActivity", "Test"+T2);
            }
            if(i % T3 == 0){
                    if(colorRight == Color.WHITE)colorRight = Color.BLACK;
                    else colorRight = Color.WHITE;
                    j++;
                    //Log.d("BouncingBallActivity", "Test"+T3);
            }
            if(i % T4 == 0){
                    if(colorBottom == Color.WHITE)colorBottom = Color.BLACK;
                    else colorBottom = Color.WHITE;
                    j++;
                    //Log.d("BouncingBallActivity", "Test"+T4);
            }
            i+=counter;
            if(j == 4)i = 0;
            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }
    
    /*public class TimerLeft extends TimerTask{
        SurfaceHolder surfaceHolder = holder;
        
        public void run(){
            
        }
    }*/
}